package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.PublicConsultAdapter;
import com.sinoglobal.sinostore.bean.CommentListVo;
import com.sinoglobal.sinostore.bean.CommentVo;
import com.sinoglobal.sinostore.system.Constants;

import java.util.ArrayList;

import andbase.listener.AbOnListViewListener;
import andbase.view.pullview.AbPullListView;


/**
 * 
* @title: PublicConsultActivity.java 
* @package com.sinoglobal.lntv.activity.shop 
* @description: TODO(咨询列表) 
* @author LK
* @date 2014-11-29 下午5:57:27
 */
public class ConsultListActivity extends StateBaseActivity implements OnClickListener,AbOnListViewListener {
	private  AbPullListView abPullListView;
	private PublicConsultAdapter adapter;
	private View shopConsuleNodata;
	
	private String goodsId;//商品id
	
	private int pageNum = 0;//当前页
	
	private int limit1 = 10;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		titleView.setText("购买咨询");
		titleRightText.setText("发表咨询");
		templateButtonRight.setOnClickListener(this);
		setContentView(R.layout.shop_activity_consultlist);
		openChenJInShi();
		initView();
		
		initData();
	}
	
	private void initData() {
		adapter = new PublicConsultAdapter(this, new ArrayList<CommentVo>());
		abPullListView.setAdapter(adapter);
		goodsId = getIntent().getStringExtra("goodsId");
		getData(pageNum,true);
	}

	private void initView() {
		abPullListView = (AbPullListView)this.findViewById(R.id.consultlist);
		abPullListView.setPullRefreshEnable(false);
		abPullListView.setPullLoadEnable(false);
		abPullListView.setAbOnListViewListener(this);
		abPullListView.setDivider(null);
		shopConsuleNodata = (View)this.findViewById(R.id.no_consult_layout);
		baseButRight.setOnClickListener(this);
		abPullListView.setEmptyView(shopConsuleNodata);
		shopConsuleNodata.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.shop_base_but_right) {
			if(isLogin()){
				Intent intent = new Intent(ConsultListActivity.this,PublicConsultActivity.class);
				intent.putExtra("goodsId", goodsId);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		} 
	}
	
	private void getData(final int pageIndex,boolean isShowProgress){
		final ProgressDialog dialog = new ProgressDialog(this);
		try {
			dialog.show();
		} catch (Exception e) {
		}
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "104");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("limit1", String.valueOf(pageNum));
		params.addQueryStringParameter("limit2", String.valueOf(limit1));
		params.addQueryStringParameter("id", goodsId);
		params.addQueryStringParameter("type", Constants.CONSULT_CODE);
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params,new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				if (!TextUtils.isEmpty(result)) {
					CommentListVo entity = JSON.parseObject(result, CommentListVo.class);
					if (Constants.STORE_SUCCESS_CODE.equals(entity.getCode())) {
						shopConsuleNodata.setVisibility(View.VISIBLE);
						if (null != entity.getJson() && entity.getJson().size() > 0) {
//							if(entity.getJson().size() < 10){
//								abPullListView.setPullLoadEnable(false);
//							}
							if(pageNum == 0){
								adapter.clearData();
							}
							adapter.addData(entity.getJson());
						} else {
							shopConsuleNodata.setVisibility(View.VISIBLE);
						}
						stopLoad();
					}
				}
			}
		});
	}

	@Override
	public void onRefresh() {
		
	}

	@Override
	public void onLoadMore() {
		pageNum++;
		getData(pageNum, false);
	}
	
	private void stopLoad() {
		abPullListView.stopRefresh();
		abPullListView.stopLoadMore();
	}
}
